/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.web.gwt.validators;

import java.util.ArrayList;

import com.google.gwt.user.client.ui.*;
import com.google.gwt.core.client.GWT;

/*
 * Base class for validators.
 */
public abstract class Validator {
	private ArrayList errorListeners;
	protected DataElement data;
	protected String msg;
	
	public Validator() {
		errorListeners = new ArrayList();
	}

	public Validator(ErrorListener e) {
		this();
		addErrorListener(e);
	}

	/*
	 * msg will be passed as an argument to the onError method of each of the listeners of this validator
	 */
	public Validator(ErrorListener e, String msg) {
		this(e);
		this.msg = msg;
	}
	
	
	protected String getWidgetValue() {
		return getWidgetValue(data.getWidget());
	}
	/*
	 * Gets the value of a widget depending on its type
	 */
	protected String getWidgetValue(Widget w) {
		
		String widgetType = GWT.getTypeName(w);
		String value = null;
		
		if(widgetType.equals("com.google.gwt.user.client.ui.TextBox")) {
			value = ((TextBox)w).getText();
		} else if(widgetType.equals("com.google.gwt.user.client.ui.ListBox")) {
			ListBox box = (ListBox)w;
			int selectedIndex = box.getSelectedIndex();
			
			if(selectedIndex != -1)
				value = box.getItemText(selectedIndex);
		} else if(widgetType.equals("com.google.gwt.user.client.ui.PasswordTextBox")) {
			value = ((PasswordTextBox)w).getText();
			
		} else {
			
			return null;	
		}
		
		if(value != null)
			value = value.trim();
		
		return value;
	}
	
	/*
	 * Any childs of this class need to implement this method. Return true if the widget passes validation, false otherwise
	 */
	public abstract boolean isValid();
	
	
	/*
	 * Attach a DataElement (widget + id) to this validator
	 */
	public void setDataElement(DataElement data) {
		this.data = data;
	}
	
	public DataElement getDataElement() {
		return data;
	}
	
	public void addErrorListener(ErrorListener e) {
		if(errorListeners != null)
			errorListeners.add(e);
	}
	
	public void removeErrorListener(ErrorListener e) {
		if(errorListeners != null)
			errorListeners.remove(e);	
	}
	
	public final boolean validate() {
		if(!isValid()) {
			for(int i = 0; i < errorListeners.size(); i++) {
				if(msg != null)
					((ErrorListener)errorListeners.get(i)).onError(data,msg);
				else
					((ErrorListener)errorListeners.get(i)).onError(data,"");
			}
			
			return false;
		}
		return true;
	}
}
